package com.atguigu.auth.controller;


import com.atguigu.auth.Service.SysRoleService;
import com.atguigu.auth.Service.SysUserService;
import com.atguigu.common.result.Result;
import com.atguigu.common.utils.MD5;
import com.atguigu.model.system.SysUser;
import com.atguigu.vo.system.AssginRoleVo;
import com.atguigu.vo.system.SysUserQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2024-08-28
 */
@Api(tags = "用户管理接口")
@RestController
@RequestMapping("/admin/system/sysUser")
public class SysUserController {
    @Autowired
    private SysUserService service;
    @ApiOperation(value = "更新状态")
    @PutMapping("/updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        service.updateStatus(id, status);
        return Result.success();
    }

    //用户条件分页查询
    @ApiOperation("用户条件分页查询")
    @GetMapping("{page}/{limit}")
    public Result   index(@PathVariable long page,
                          @PathVariable long limit,
                          SysUserQueryVo sysUserQueryVo){
        //创建对象
        Page<SysUser> pageParam = new Page<>(page, limit);
        //判断条件  条件值不为空
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
//        从vu获取条件值
        String   username = sysUserQueryVo.getKeyword();
        String createTimeBegin = sysUserQueryVo.getCreateTimeBegin();
        String createTimeEnd = sysUserQueryVo.getCreateTimeEnd();
//        判断条件值是否为空
        //like代表模糊查询
        
        if (!StringUtils.isEmpty(username)){
            wrapper.like(SysUser::getUsername, username);
        }
//        ge大于等于
        if (!StringUtils.isEmpty(createTimeBegin)){
            wrapper.ge(SysUser::getCreateTime, createTimeBegin);
        }
//        le小于等于
        if (!StringUtils.isEmpty(createTimeEnd)){
            wrapper.lt(SysUser::getCreateTime, createTimeEnd);
        }
//        调用mp方法实现条件 分页查询
        Page<SysUser> pageMode1 = service.page(pageParam, wrapper);

        return Result.success(pageMode1);
    }

//    增删改查

//    根据id获取用户
    @ApiOperation(value = "获取用户")
    @GetMapping("get/{id}")
    public Result get(@PathVariable long id){
        SysUser user = service.getById(id);
        return Result.success(user);
    }
    //保存用户
    @ApiOperation(value = "添加用户")
    @PostMapping("save")
    public Result save(@RequestBody SysUser user){
//        密码加密存储
        String password = user.getPassword();
        String passwordMD5 = MD5.encrypt(password);
        user.setPassword(passwordMD5);
        service.save(user);
        return Result.success();
    }
    //更新用户
    @ApiOperation("更新用户")
    @PutMapping("update")
    public Result update(@RequestBody SysUser user){
        service.updateById(user);
        return Result.success();
    }
    //删除用户
    @ApiOperation(value = "删除用户")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        service.removeById(id);
        return Result.success();
    }

}

